package com.controller;

import com.service.MenuService;
import com.util.AjaxResult;
import com.util.Empty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Length;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@RequestMapping("/menu")
@Api(tags = {"菜单"})
public class MenuController {

    private final static Logger log = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    MenuService menuService;

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public AjaxResult save(
            @ApiParam(value = "上级菜单id")
            @RequestParam(name = "pid",required = false)
            @Length(max = 32) String pid,
            @ApiParam(value = "菜单名称", required = true)
            @RequestParam(name = "menu_name",required = false)
            @Empty(max = 16) String menu_name,
            @ApiParam(value = "菜单图标")
            @RequestParam(name = "menu_icon",defaultValue = "BellFilled",required = false)
            @Empty(max = 64) String menu_icon,
            @ApiParam(value = "菜单路径")
            @RequestParam(name = "menu_uri",required = false)
            @Length(max = 128) String menu_uri,
            @ApiParam(value = "类型(1-目录2-按钮)", required = true)
            @RequestParam(name = "menu_type")
            @Empty(max = 1) String menu_type,
            @ApiParam(value = "序号")
            @RequestParam(name = "menu_order")
            @Length(max = 3) String menu_order){
        return menuService.save(pid,menu_name,menu_icon,menu_uri,menu_type,menu_order);
    }

    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public AjaxResult update(
            @ApiParam(value = "菜单id", required = true)
            @RequestParam(name = "menu_id")
            @Length(max = 32) String menu_id,
            @ApiParam(value = "上级菜单id")
            @RequestParam(name = "pid",required = false)
            @Length(max = 32) String pid,
            @ApiParam(value = "菜单名称", required = true)
            @RequestParam(name = "menu_name",required = false)
            @Empty(max = 16) String menu_name,
            @ApiParam(value = "菜单图标", required = true)
            @RequestParam(name = "menu_icon",defaultValue = "BellFilled",required = false)
            @Empty(max = 64) String menu_icon,
            @ApiParam(value = "菜单路径")
            @RequestParam(name = "menu_uri",required = false)
            @Length(max = 128) String menu_uri,
            @ApiParam(value = "类型(1-目录2-按钮)", required = true)
            @RequestParam(name = "menu_type",required = false)
            @Empty(max = 1) String menu_type,
            @ApiParam(value = "序号")
            @RequestParam(name = "menu_order")
            @Length(max = 3) String menu_order){
        return menuService.update(menu_id,pid,menu_name,menu_icon,menu_uri,menu_type,menu_order);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/del")
    public AjaxResult del(@ApiParam(value = "菜单id")
                          @RequestParam(name = "menu_id",required = false)
                          @Empty(max = 32) String menu_id){
        return menuService.del(menu_id);
    }

    @ApiOperation(value = "分页列表")
    @PostMapping("/list")
    public AjaxResult list(@ApiParam(value = "菜单名称")
                           @Length(max = 16) String menu_name,
                           @ApiParam(value = "菜单路径")
                           @Length(max = 128) String menu_uri){
        return menuService.list(menu_name,menu_uri);
    }

    @ApiOperation(value = "全部下拉列表")
    @GetMapping("/selectAll")
    public AjaxResult selectAll(){
        return menuService.selectAll();
    }

}
